<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item style="float:right;">
          <el-button size="small" icon="plus" type="primary" v-show="productCompanyAdd" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="productCompany" stripe border highlight-current-row :height="winHeight-188" v-loading="listLoading" @selection-change="selsChange" @cell-click="dblclick" style="width: 100%;">
      <el-table-column prop="codeCname" label="公司名称" align="center">
      </el-table-column>

      <el-table-column label="操作" fixed='right' width="150">
        <template scope="scope">
          <el-button size="small" v-show="productCompanyUpdate" type="warning" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button type="danger" v-show='productCompanyRemove' size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->

      <el-pagination style="float:right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 40, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>

    <!--新增界面-->
    <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
      <el-form label-width="100px" :model="addForm" :rules="addFormRules" ref="addForm">
        <el-row>
          <el-col :xs="24" :sm="20" :md="12" :lg="12">
            <el-form-item label="公司名称:" prop="codeCname">
              <el-input v-model="addForm.codeCname" auto-complete="off">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog title="修改" v-model="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
        <el-row>
          <el-col :xs="24" :sm="20" :md="12" :lg="12">
            <el-form-item label="公司名称:" prop="codeCname">
              <el-input v-model="editForm.codeCname" auto-complete="off">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import util from '../../common/js/util'
import { mapState, mapActions } from 'vuex'
import commons from '../../common/js/common'
import { prodeucCompanyList, addProductCompany, deleteProductCompany } from '../../api/api';
export default {
  data() {
    return {
      filters: {
        codeCname: ''
      },
      exportTotal: 0,
      loading: '',
      total: 0,
      page: 1,
      pageSize: 10,
      listLoading: false,
      sels: [],//列表选中列
      addFormVisible: false,//新增界面是否显示
      editFormVisible: false,//修改界面是否显示
      editLoading: false,
      productCompany: [],
      productCompanyAdd: false,
      productCompanyUpdate: false,
      productCompanyRemove: false,

      //修改界面数据
      editForm: {
        id: '',
        codeCname: ''
      },
      detailFormVisible: false,//新增界面是否显示
      addLoading: false,
      IsShow: false,
      detailFormRules: {
      },
      //新增界面数据
      addForm: {
        codeCname: ''
      }

    }
  },
  computed: mapState({
    clist: 'riskCodelist',
    winHeight: 'winHeight'
  }),
  methods: {
    handleCurrentChange(val) {
      this.page = val;
      this.getProductCompany(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getProductCompany(true);
    },
    //获取列表
    getProductCompany(bool) {
      let para = new FormData();
      para.append("pageSize", bool ? this.pageSize : this.exportTotal);
      para.append("pageNumber", bool ? this.page : 1);
      this.listLoading = bool;
      prodeucCompanyList(para).then((res) => {
        console.log(res);
        this.lists = res.data.result.list;
        this.total = res.data.result.total;
        this.exportTotal = res.data.result.exportTotal;
        this.listLoading = false;
        if (!bool) {
          this.export2Excel();
          this.loading.close();
        } else {
          this.productCompany = res.data.result.list;
        }
      });
    },
    //详情
    dblclick(row, column, cell, event) {
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let para = new FormData();
        para.append("id", row.id);
        deleteProductCompany(para).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getProductCompany(true);
        });
      }).catch(() => {

      });
    },

    //table导出excel
    exportExcels: function () {
      this.getProductCompany(false);
    },
    export2Excel: function () {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../vendor/Export2Excel');
        const tHeader = [''];
        const filterVal = [''];
        const list = this.lists;
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '列表excel');
      })
    },
    formatJson: function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },

    //显示修改界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      this.editForm.id = row.id;
      this.editForm.codeCname = row.codeCname;
    },
    //显示新增界面
    handleAdd: function () {
      this.addFormVisible = true;
      this.addForm = {
        codeCname: ''
      };
    },
    //新增提交
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            let para = this.addForm;
            addProductCompany(para).then((res) => {
              this.addLoading = false;
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              this.$refs['addForm'].resetFields();
              this.addFormVisible = false;
              this.getProductCompany(true);
            });
          });
        }
      });
    },
    //修改
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            let para = this.editForm;

            addProductCompany(para).then((res) => {
              if (res.data.success) {
                this.editLoading = false;
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['editForm'].resetFields();
                this.editFormVisible = false;
                this.getProductCompany(true);
              }
              else {
                this.editLoading = false;
                this.$message({
                  message: res.data.result,
                  type: 'error'
                });
                this.editFormVisible = false;
              }
            }).catch(() => {
              this.editLoading = false;
            });
          });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let para = new FormData();
        para.append("ids", ids);
        removeAd(para).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getProductCompany(true);
        });
      }).catch(() => {

      });
    },
    btnShow: function () {
      let thisData = JSON.parse(sessionStorage.getItem('159'));
      let _this = this;
      $.each(thisData, function (i, j) {
        if (thisData[i] == "productCompanyAdd") {
          _this.productCompanyAdd = true;
        }
        if (thisData[i] == "productCompanyRemove") {
          _this.productCompanyRemove = true;
        }
        if (thisData[i] == "productCompanyUpdate") {
          _this.productCompanyUpdate = true;
        }
      });
    }
  },
  mounted() {
    this.getProductCompany(true);
    this.btnShow();
  }
}

</script>

<style scoped>
</style>